package com.ck.ips.controller;

import com.ck.common.model.OperationResult;
import com.ck.core.annotation.LoginRequired;
import com.ck.core.constant.CharsetConstant;
import com.ck.ips.model.vo.sys.BrandVO;
import com.ck.ips.service.sys.BrandService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 品牌管理控制层
 *
 * @author XC17125
 */
@RestController
@LoginRequired
@RequestMapping(value = "/brand")
public class BrandController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Autowired
    BrandService brandService;

    /**
     * 添加品牌
     */
    @PostMapping("/add")
    public Object addBrand(@RequestBody @Validated BrandVO brand) {
        if (brand.getBrandId() != null) {
            logger.info("添加品牌信息失败，品牌id非空，请求的参数为{}", brand.toString());
            throw new IllegalArgumentException("请求接口错误，品牌id非空，需请求编辑接口！");
        }
        try {
            brandService.addBrand(brand);
        } catch (Exception e) {
            logger.info("添加品牌信息失败，失败原因{}，请求参数{}", e.getMessage(), brand.toString());
            return OperationResult.newFailOperationResult(e.getMessage());
        }
        return OperationResult.newSuccessOperationResult();
    }

    /**
     * 编辑品牌
     */
    @PostMapping("/edit")
    public Object editBrand(@RequestBody @Validated BrandVO brand) {
        if (brand.getBrandId() == null) {
            logger.info("编辑品牌信息失败，品牌id为空，请求的参数为{}", brand.toString());
            throw new IllegalArgumentException("编辑品牌信息失败，品牌id为空，请检查参数！");
        }
        try {
            return brandService.editBrand(brand);
        } catch (Exception e) {
            logger.info("编辑品牌信息失败，失败原因{}，请求参数{}", e.getMessage(), brand.toString());
            return OperationResult.newFailOperationResult(e.getMessage());
        }
    }

    /**
     * 删除品牌
     */
    @PostMapping("/del")
    public Object delBrand(@RequestBody BrandVO brand) {
        if (brand.getBrandId() == null) {
            logger.info("删除品牌信息失败，品牌id为空，请求的参数为{}", brand.toString());
            throw new IllegalArgumentException("删除品牌信息失败，品牌id为空，请检查参数！");
        }
        try {
            return brandService.delBrand(brand);
        } catch (Exception e) {
            logger.info("删除品牌信息失败，失败原因{}，请求参数{}", e.getMessage(), brand.toString());
            return OperationResult.newFailOperationResult(e.getMessage());
        }
    }

    /**
     * 查询品牌列表
     */
    @PostMapping("/list")
    public Object listBrand(@RequestBody BrandVO brand) {
        return brandService.listBrand(brand);
    }

    /**
     * 查询品牌列表
     */
    @GetMapping("/listAllBrands")
    public Object listAllBrands() {
        return brandService.listAllBrands();
    }

    /**
     * 导出终端excel
     */
    @GetMapping(value = "/exportBrandExcel")
    public void exportBrandExcel(HttpServletResponse response, String brandName, String contactPerson, String phoneNum) throws IOException {
        //装入excel表中
        XSSFWorkbook workbook = brandService.exportBrandExcel(brandName, contactPerson, phoneNum);
        //准备将Excel的输出流通过response输出到页面下载
        //输出流
        response.setContentType("application/ms-excel");
        response.setCharacterEncoding(CharsetConstant.UTF8);
        //这后面可以设置导出Excel的名称，此例中名为品牌列表.xls
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("品牌列表.xlsx", CharsetConstant.UTF8));
        //刷新缓冲
        response.flushBuffer();
        //workbook将Excel写入到response的输出流中，供页面下载
        workbook.write(response.getOutputStream());
    }

}
